package mandelbrot;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

import client.Client;

import comm.ClientPriority;

public class MandelbrotClient {

	private Client client;

	
	private int maxCount = 8192;
	private boolean smooth = false;
	private boolean antialias = false;
	private boolean drag = false;
	private boolean toDrag = false;
	private boolean rect = true, oldRect = true;
	private Color[][] colors;
	private int pal = 0;
	private double viewX = 0.0;
	private double viewY = 0.0;
	private double zoom = 1.0;

	private int width  = 8000;
	private int height = 8000;

	private int boxHeight = 200;
	private int boxWidth  = 200;

	

	private JFrame frame;
	private BufferedImage image;

	private static final int[][][] colpal = {
		{ {0, 10, 20}, {50, 100, 240}, {20, 3, 26}, {230, 60, 20},
			{25, 10, 9}, {230, 170, 0}, {20, 40, 10}, {0, 100, 0},
			{5, 10, 10}, {210, 70, 30}, {90, 0, 50}, {180, 90, 120},
			{0, 20, 40}, {30, 70, 200} },
			{ {70, 0, 20}, {100, 0, 100}, {255, 0, 0}, {255, 200, 0} },
			{ {40, 70, 10}, {40, 170, 10}, {100, 255, 70}, {255, 255, 255} },
			{ {0, 0, 0}, {0, 0, 255}, {0, 255, 255}, {255, 255, 255}, {0, 128, 255} },
			{ {0, 0, 0}, {255, 255, 255}, {128, 128, 128} },
	};




//	public Mandelbrot() {
//
//
//		
//		long totalTime = (System.currentTimeMillis() - startTime); 
//		System.out.println("Total time:        " + (double) totalTime / 1000.0 + " seconds");
//		System.out.println("Average box time:  " + (double) totalTime / (double) numberOfBoxes);
//		System.out.println("Shortest box time: " + ((double) shortestBoxTime) / 1000.0 + " seconds");
//		System.out.println("Longest box time:  " + ((double) longestBoxTime) / 1000.0 + " seconds");
//	}


	
	
	
	private void init() {

//		client = new Client(ClientPriority.NORMAL, 20, 2, "localhost", 0,
//				"localhost", 0);
		client = new Client(ClientPriority.LOW, 20, 2, "192.168.1.7", 0,
				"192.168.1.7", 0);

		long time = System.currentTimeMillis();
		System.out.println("Started at:" + time);
		
		
		// initialize color palettes
		colors = new Color[colpal.length][];
		for (int p = 0; p < colpal.length; p++) {
			colors[p] = new Color[colpal[p].length * 12];
			for (int i = 0; i < colpal[p].length; i++) {
				int[] c1 = colpal[p][i];
				int[] c2 = colpal[p][(i + 1) % colpal[p].length];
				for (int j = 0; j < 12; j++)
					colors[p][i * 12 + j] = new Color(
							(c1[0] * (11 - j) + c2[0] * j) / 11,
							(c1[1] * (11 - j) + c2[1] * j) / 11,
							(c1[2] * (11 - j) + c2[2] * j) / 11);
			}
		}

		image = new BufferedImage((int) width, (int) height, BufferedImage.TYPE_INT_RGB);

		

		new Thread(new Runnable() {

			@Override
			public void run() {
				int  currentBox = 0;
				for (int by = 0; by < height / boxHeight; by++) {
					for (int bx = 0; bx < width / boxWidth; bx++) {
						client.commit(new MandelbrotTask(currentBox++,by,bx,boxWidth,boxHeight,zoom,width,height,viewX,viewY,colors,maxCount,System.currentTimeMillis()));						
					}
				}

			}

		}).start();

		Graphics2D g2d = (Graphics2D)image.getGraphics();
		int numberOfBoxes = height / boxHeight * width / boxWidth;

		for (int i = 0; i < numberOfBoxes ; i++) {
			MandelbrotResult result = (MandelbrotResult) client.getNextResult();
			ImageIcon im =  new ImageIcon(result.getImage());
			Image source = im.getImage();
			g2d.drawImage(source, result.getX()*boxWidth, result.getY()*boxHeight, null);
			System.out.println("Got: "+result.getId() + " took:"+result.getTime() +
					" was in the system:"+(System.currentTimeMillis()-result.getInitTime()));
		}
		try {
			FileOutputStream out = new FileOutputStream("result.jpg");
			ImageIO.write(image,
					"jpg",
					out);
			out.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


		System.out.println("Total time:" + (System.currentTimeMillis() - time));
		System.out.println("Ended at:" + System.currentTimeMillis());
		System.exit(0);

	}

	public static void main(String[] args) {
		new MandelbrotClient().init();

	}

}
